/* ********************************************************************************* */
/* 9 ********************* Joint Ventures & Alliances ****************************** */
/* ********************************************************************************* */
/* Summary   : Code builds dataset for JV & SA test. JV & SA data from SDC           */
/* ********************************************************************************* */

	LIBNAME cow 'C:\cow';
	LIBNAME data 'C:\data';


*** get Strategic Alliance sample from SDC, save as SAS file alliance;
	data alliance; set data.alliance;
	d_alliance=1; if alliancestatus in ("C","Extended"); if year(alliancedate)>=1994;
	run;

*** get Joint Venture sample from SDC, save as SAS file jv2;
	data jv; set data.jv; format jvdate date9.; if year(jvdate)~=1899; d_jv=1; if year(jvdate)>=1994;
	if jvstatus in ("C", "Extended", "Terminated" ); if year(jvdateend)=1899 then jvdateend=mdy(12,31,2018); run;

* use firm-year panel data set from SAS program file "1 mvc.sas";
	data mvc_all; set cow.mvc_all(keep=permno--cik); if fyear>=1995; proc sort; by permno gvkey fyear; run;

* merge JV & SA into firm-year panel;
	proc sql; create table cow_a as select * from mvc_all as a left join alliance as b 
	on (substr(a.cusip,1,6)=b.alliancecusip1 or substr(a.cusip,1,6)=b.alliancecusip2 or substr(a.cusip,1,6)=b.alliancecusip3 or 
	   substr(a.cusip,1,6)=b.alliancecusip4) and a.datadate<b.alliancedate<a.datadate+365; quit;
	proc sort data=cow_a; by permno gvkey fyear descending d_alliance; run; 
	data cow_sa; set cow_a; by permno gvkey fyear; if first.permno or first.gvkey or first.fyear; run;

	proc sql; create table cow_j as select * from cow_sa as a left join jv as b 
	on (substr(a.cusip,1,6)=b.jvcusip1 or substr(a.cusip,1,6)=b.jvcusip2 or substr(a.cusip,1,6)=b.jvcusip3 or 
	   substr(a.cusip,1,6)=b.jvcusip4) and a.datadate<b.jvdate<a.datadate+365; quit;
	proc sort data=cow_j; by permno gvkey fyear descending d_jv; run; 
	data cow_jv; set cow_j; by permno gvkey fyear; if first.permno or first.gvkey or first.fyear; run;

* use data files created from previous programs;
	proc sql; create table comp_ma_temp_1 as select * from cow_jv as a left join data.allcrspcomp as b 
	on a.cusip=b.cusip and a.fyear=b.fyear; quit; 
	data firmage; set data.comp; keep cusip fyear firmage; run;
	proc sql; create table comp_ma_temp_2 as select * from comp_ma_temp_1 as a left join firmage as b 
	on a.cusip=b.cusip and a.fyear=b.fyear; quit; 
	data illiq_cusip; set data.illiq_cusip; keep cusip datadate illiq; run;
	proc sql; create table comp_ma as select * from comp_ma_temp_2 as a left join illiq_cusip as b 
	on a.datadate=b.datadate and a.cusip=b.cusip; quit;
	proc sort; by row descending illiq; run;
	data comp_jvsa; set comp_ma; by row; if first.row;
	if d_jv=. then d_jv=0; 	if d_alliance=. then d_alliance=0; 
	if d_jv=1 or d_alliance=1 then d_jvsa=1; else d_jvsa=0;
	ind1d=floor(sic/1000); ind1d_fyear=ind1d+fyear*100; 
	ind2d=floor(sic/100);	ind2d_fyear=ind2d+fyear*100;
	if 1996<=fyear<=2018;
	run;

	PROC EXPORT DATA= comp_jvsa OUTFILE= "C:\cow\comp_jvsa.dta" DBMS=STATA REPLACE;RUN;

